Skip to content

Share equal recoupling matrices between tree transformers - #630

Merged
lkdvos merged 5 commits into
mainfrom
lb/share_recoupling_matrices
Nov 22, 2026
Merged

lkdvos merged 5 commits into
mainfrom
lb/share_recoupling_matrices

Conversation

@leburgel

@leburgel leburgel commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Tree transformers for sectors with multiple fusion channels store one recoupling matrix per fusion tree block. These matrices only depend on the sectors, but the transformers did not share them: for product sectors such as FermionParity ⊠ U1Irrep ⊠ SU2Irrep, product_transform assembles a new matrix on every call, blocks that differ only in their abelian charges have equal matrices under different cache keys, and the cached braids and transposes were converted to the scalar type of the transformer, which copies whenever the two differ.

This caches the braids and transposes of fusion tree blocks in the scalar type of the transformers that use them, and returns their matrices through recouplingmatrix, which gives back an equal matrix seen before, so that all transformers share one copy. cachesize of a transformer leaves the shared matrices out, since the cache of recouplingmatrix counts them.

After one gradient of a D = 8 fermionic SU(2) PEPS (FermionParity ⊠ U1Irrep ⊠ SU2Irrep), the treebraider cache held 12.8 million recoupling matrices, of which 164338 were distinct: 2.21 GiB before, 0.085 GiB with this change. For all 480 permutations of a small tensor of the same kind, TensorKit's caches go from 89.5 to 76.6 MiB, and the transformers keep 7689 distinct matrices instead of a copy for each of their 681138 blocks when the same permutations are applied to three spaces. The D = 8 numbers were measured with the matrices shared at the level of the transformers instead, which then hold the same matrices as here.

Reproducer
using TensorKit, Printf
using TensorKit: MemoizationKit

s(p, n, j) = FermionParity(p) ⊠ U1Irrep(n) ⊠ SU2Irrep(j)
I = typeof(s(0, 0, 0))
P = Vect[I](s(0, 0, 0) => 1, s(1, 1, 1 // 2) => 1, s(0, 2, 0) => 1)
V = Vect[I](s(0, 0, 0) => 2, s(1, 1, 1 // 2) => 1, s(1, -1, 1 // 2) => 1, s(0, 2, 0) => 1, s(0, -2, 0) => 1, s(0, 0, 1) => 1)
t = randn(P ⊗ V ⊗ V ← V ⊗ V)
perms = [(p[1:k], p[(k + 1):end]) for p in Iterators.product(ntuple(_ -> 1:5, 5)...) if allunique(p) for k in 1:4]
permuteall(t) = foreach(p -> permute(t, p), perms)

cachedvalues() = [v for (_, c) in MemoizationKit.cache_info(TensorKit) for (_, v) in c]
permuteall(t) # compile
empty_globalcaches!(); GC.gc()
cold = @elapsed permuteall(t)
warm = @elapsed permuteall(t)
vs = cachedvalues()
@printf("%d permutations, %d cached values\n", length(perms), length(vs))
@printf("TensorKit caches: %.1f MiB (summarysize, shared arrays once), %.1f MiB (sum of cachesize)\n",
    Base.summarysize(vs) / 2^20, sum(v -> MemoizationKit.cachesize(v), vs) / 2^20)
@printf("permutations from empty caches %.2f s, with full caches %.3f s\n", cold, warm)

main:

480 permutations, 10719 cached values
TensorKit caches: 89.5 MiB (summarysize, shared arrays once), 86.3 MiB (sum of cachesize)
permutations from empty caches 1.58 s, with full caches 0.152 s

this PR, at its first commit (76.6 MiB for the final form, measured with the same tensor and permutations):

480 permutations, 18408 cached values
TensorKit caches: 76.8 MiB (summarysize, shared arrays once), 83.9 MiB (sum of cachesize)
permutations from empty caches 1.51 s, with full caches 0.148 s

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea, however I was confused that this wasn't already the case. The problem is that the transform typically already caches the results, but in the latest versions I've added this convert(Matrix{T}, U) which creates an independent copy. It might be better to immediately fix the transform call to take in the desired eltype and cache that version, because effectively we will now be storing both a real and complex version in many cases (I think)

@leburgel

Copy link
Copy Markdown
Member Author

The convert only copies when the precision differs (e.g. Float32 storage), since recoupling_scalartype keeps real coefficients real for Float64/ComplexF64 storage, so in the common case the transformer already holds the cached matrix itself. The duplicates come from elsewhere: for product sectors (FermionParity ⊠ U1Irrep ⊠ SU2Irrep in the PEPS case) product_transform assembles a fresh Kronecker product on every call, and blocks that differ only in their abelian charges have equal matrices under different fusiontreebraid keys, so those are only shared by a cache keyed by value. That is where the 12.8 million matrices with 164338 distinct came from. Passing the eltype to transform would still remove the Float32 copies; happy to do that here or separately.

@lkdvos

lkdvos commented Nov 20, 2026

Copy link
Copy Markdown
Member

Ah I see, but in that case it might make more sense to simply have the product transformations also cached, which should achieve the same thing? Let's keep the element type for future changes, since that touches quite a bit more code

…type of the transformers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leburgel

Copy link
Copy Markdown
Member Author

Caching the product transforms would share the matrix between transformers that braid the same block in the same way, but not between blocks that differ only in their abelian charges, which have equal matrices under different keys; I don't know yet how the duplicates in the PEPS gradient split between the two. It would also add to the fusiontreebraid cache, which already needed 345648 entries for the factor transforms alone in that gradient, against its bound of 10^4, whereas the 164338 distinct matrices took 0.085 GiB. I can measure the product transforms cached as an alternative if you like. Agreed on leaving the eltype for later.

…he tree transformers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ext/TensorKitEnzymeExt/utility.jl 0.00% 2 Missing ⚠️
src/fusiontrees/braiding_manipulations.jl 92.85% 1 Missing ⚠️
src/fusiontrees/duality_manipulations.jl 92.85% 1 Missing ⚠️
Files with missing lines Coverage Δ
src/fusiontrees/fusiontrees.jl 95.77% <100.00%> (+0.06%) ⬆️
src/tensors/treetransformers.jl 96.71% <100.00%> (+0.71%) ⬆️
src/fusiontrees/braiding_manipulations.jl 95.88% <92.85%> (+0.39%) ⬆️
src/fusiontrees/duality_manipulations.jl 98.68% <92.85%> (+0.27%) ⬆️
ext/TensorKitEnzymeExt/utility.jl 0.00% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks great, the only remark I have is that it's slightly inconsistent whether you put the T argument at the front or the end of the argument list. I don't have a strong preference on which one, I'd just keep it consistent

Comment thread src/fusiontrees/fusiontrees.jl Resolved
…return type of recouplingmatrix

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leburgel

leburgel commented Nov 22, 2026 •

Copy link
Copy Markdown
Member Author

Made the scalar type the first argument throughout (fusiontreebraid(T, src, p, levels), fusiontreetranspose(T, src, p) and their result types, as in _braid and _transpose), and annotated the return type of recouplingmatrix. @cached works on single-line definitions, but with the annotation the where {T} binds to the return type, so it is in long form now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lkdvos

lkdvos commented Nov 22, 2026

Copy link
Copy Markdown
Member

CI failures seem unrelated, will merge

@lkdvos
lkdvos merged commit 2ab9c1c into main Nov 22, 2026
19 of 22 checks passed
@lkdvos
lkdvos deleted the lb/share_recoupling_matrices branch November 22, 2026 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants